package com.wxxymaker.edu.hook;

import com.blade.ioc.Ioc;
import com.blade.mvc.RouteContext;
import com.blade.mvc.WebContext;
import com.blade.mvc.hook.WebHook;
import com.blade.mvc.ui.ModelAndView;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.roles.CourseLearnRoles;
import com.wxxymaker.edu.service.CourseService;
import com.wxxymaker.edu.service.LearnCourseService;
import com.wxxymaker.edu.service.TaskService;
import com.wxxymaker.edu.utils.HttpUtil;
import com.wxxymaker.edu.utils.TokenUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * @AUTHOR soft
 * @DATE 2018/9/9 10:45
 * @DESCRIBE 课程拦截器 拦截课程的存在，状态，学生的学习
 * 没有加入学习的课程并且不是自己发布的课程 查看不了加入学习页面
 * isMyPublish: 判读是不是当前教师发布的课程
 *
 * 加入课程存在验证，课程id不存在则不能访问
 * 加入任务状态判断
 */
@Slf4j
public class CourseInfoHook implements WebHook {

    @Override
    public boolean before(RouteContext ctx) {
        try {
            String uri = ctx.uri();
            if (uri.startsWith("/course") && !uri.contains("/set/")) {
                Integer       kid           = ctx.pathInt("kid");
                if (kid == null) return true;
                Ioc           ioc           = WebContext.blade().ioc();
                CourseService courseService = ioc.getBean(CourseService.class);
                ctx.attribute("kid", kid);

                if (!courseService.has(kid)) {
                    ModelAndView view = new ModelAndView(HttpUtil._400());
                    view.add("msg", "课程不存!");
                    ctx.render(view);
                    return false;
                }

                User    user      = TokenUtils.currentUser();
                boolean publish   = courseService.isPublish(kid);
                boolean myPublish = false;
                if (user != null) {
                    myPublish = courseService.isMyPublish(kid, user.getId());
                }
                ctx.attribute("isMyPublish", myPublish);
                if (user != null && user.getRole() >= 3) return true; // 超级管理直接放行
                if (myPublish) return true;

                if (!publish) { // 课程未发布
                    ModelAndView view = new ModelAndView(HttpUtil._400());
                    view.add("msg", "无权限查看该课程信息!");
                    ctx.render(view);
                    return false;
                }

                Integer tid = ctx.pathInt("tid"); // 判断任务发布
                if (tid != null && tid != 0) {
                    TaskService taskService = ioc.getBean(TaskService.class);
                    if (!taskService.isPublish(tid)) { // 任务未发布
                        ModelAndView view = new ModelAndView(HttpUtil._400());
                        view.add("msg", "无权限查看该任务信息!");
                        ctx.render(view);
                        return false;
                    }
                }

                CourseLearnRoles joinLearnRoles = ctx.routeAction().getAnnotation(CourseLearnRoles.class);
                if (joinLearnRoles == null) {
                    joinLearnRoles = ctx.routeTarget().getClass().getAnnotation(CourseLearnRoles.class);
                }
                if (user != null && joinLearnRoles != null) {
                    LearnCourseService learnCourseService = ioc.getBean(LearnCourseService.class);
                    if (learnCourseService.isJoin(kid, user.getId())) { // 学生应当加入学习
                        return true;
                    }
                    ctx.redirect("/course/" + kid + "/info");
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            log.error("出错500！{}", e.getMessage());
            ModelAndView view = new ModelAndView(HttpUtil._500());
            view.add("msg", e.getMessage());
            ctx.render(view);
            return false;
        }
    }
}
